//
//  LLRefreshNomalHeader.m
//  PandaMakeUp
//
//  Created by dl on 16/1/12.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLRefreshNomalHeader.h"
#import "LLLoadingHeaderView.h"
#import "LLRefreshConst.h"

#define LLRefreshNomalHeaderHeight           FULL_HEIGHT
#define LLRefreshPullCriticalHeight          SUIT_H(76)                 // 下拉刷新的临界点

@interface LLRefreshNomalHeader ()
@property(nonatomic, weak) LLLoadingHeaderView *headerView;
@end

@implementation LLRefreshNomalHeader {
    __weak LLLoadingHeaderView *_headerView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        LLLoadingHeaderView *headerView = [LLLoadingHeaderView new];
        [self addSubview:_headerView = headerView];

        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.centerX.width.equalTo(self);
            make.height.mas_equalTo(LLRefreshNomalHeaderHeight);
        }];
    }
    return self;
}

+ (instancetype)headerWithRefreshingBlock:(LLRefreshComponentRefreshingBlock)refreshingBlock {
    LLRefreshNomalHeader *cmp = [[self alloc] init];
    cmp.refreshingBlock = refreshingBlock;
    return cmp;
}

#pragma mark - 覆盖父类的方法

- (void)prepare {
    [super prepare];

    self.ll_height = LLRefreshNomalHeaderHeight;
    self.layer.masksToBounds = YES;
    [self.superview insertSubview:self atIndex:0];
}

- (void)placeSubviews {
    [super placeSubviews];

    // 设置y值(当自己的高度发生改变了，肯定要重新调整Y值，所以放到placeSubviews方法中设置y值)
    self.ll_top = -self.ll_height;
}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change {
    [super scrollViewContentOffsetDidChange:change];

    // 在刷新的refreshing状态
    if (self.state == LLRefreshStateRefreshing) {
        // sectionheader停留解决
        return;
    }

    // 跳转到下一个控制器时，contentInset可能会变
    _scrollViewOriginalInset = self.scrollView.contentInset;

    // 当前的contentOffset
    CGFloat offsetY = self.scrollView.contentOffset.y;
    // 头部控件刚好出现的offsetY
    CGFloat happenOffsetY = -self.scrollViewOriginalInset.top;

    // 如果是向上滚动到看不见头部控件，直接返回
    // >= -> >
    if (offsetY > happenOffsetY) return;

    // 普通 和 即将刷新 的临界点
    CGFloat normal2pullingOffsetY = happenOffsetY - LLRefreshPullCriticalHeight;
    CGFloat pullingPercent = (happenOffsetY - offsetY) / LLRefreshPullCriticalHeight;

    if (self.scrollView.isDragging) { // 如果正在拖拽
        self.pullingPercent = pullingPercent;
        if (self.state == LLRefreshStateIdle && offsetY < normal2pullingOffsetY) {
            // 转为即将刷新状态
            self.state = LLRefreshStatePulling;
        } else if (self.state == LLRefreshStatePulling && offsetY >= normal2pullingOffsetY) {
            // 转为普通状态
            self.state = LLRefreshStateIdle;
        }
    } else if (self.state == LLRefreshStatePulling) {// 即将刷新 && 手松开
        // 开始刷新
        [self beginRefreshing];
    } else if (pullingPercent < 1) {
        self.pullingPercent = pullingPercent;
    }
}

- (void)setState:(LLRefreshState)state {
    LLRefreshCheckState

    // 根据状态做事情
    if (state == LLRefreshStateIdle) {
        if (oldState != LLRefreshStateRefreshing) return;

        // 恢复inset和offset
        UIEdgeInsets inset = self.scrollView.contentInset;
        inset.top -= LLRefreshHeaderHeight;
        [UIView animateWithDuration:LLRefreshSlowAnimationDuration animations:^{
            self.scrollView.contentInset = inset;
        }                completion:^(BOOL finished) {
            self.pullingPercent = 0.0;
        }];
    } else if (state == LLRefreshStateRefreshing) {
        UIEdgeInsets inset = self.scrollView.contentInset;
        inset.top += LLRefreshHeaderHeight;
        [UIView animateWithDuration:LLRefreshFastAnimationDuration animations:^{
            self.scrollView.contentInset = inset;
        }                completion:^(BOOL finished) {
            [self executeRefreshingCallback];
        }];
    }
}

#pragma mark - 公共方法

- (void)beginRefreshing {
    [super beginRefreshing];
}

@end
